<template>
  <a-card :bordered="false">
    
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" v-show="false" icon="plus">新增</a-button>
      <!--<a-button type="primary" icon="download" @click="handleExportXls('分类字典')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
    	
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :expandedRowKeys="expandedRowKeys"
        @change="handleTableChange"
        @expand="handleExpand"
     
>
        
		    <span slot="tags" slot-scope="tags">
		    	    <div v-if="tags==1">
					      <a-tag color="blue" >正常</a-tag>
					    </div>
					    <div v-else>
					      <a-tag color="red" >异常</a-tag>
					    </div>	
		    </span>
<!--		    
		    <span slot="tags1" slot-scope="tags1">
		    	    <div v-if="tags1==1">
					      <a-tag color="blue" >上架</a-tag>
					    </div>
					    <div v-else>
					      <a-tag color="#CCCCCC" >下架</a-tag>
					    </div>	
		    </span>   
		    
-->		    
        <span slot="action" slot-scope="text, record">
        	<a @click="showDeviceInfo(record)">详情</a>
          <a-divider type="vertical" />
          <!--<a @click="handleEdit(record)">上架、下架</a>-->
         
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>
        <!--展开的子表 -->
        <div
        slot="expandedRowRender"
        slot-scope="record"
        style="margin: 0">
                <a-row
          :gutter="24"
          :style="{ marginBottom: '12px' }">
          
          <a-col :span="112" >设备启动时间：     {{record.deviceStartTime}}</a-col>
          <a-col :span="112" >设备运行时间 ： {{record.deviceRunTime}}</a-col>
				  <a-col :span="112" > 备板类型：  {{record.bkpBoardType}}</a-col>      
				  <a-col :span="112" >备板编码：      {{record.bkpPcbVersion}}</a-col>   
				  <a-col :span="112" >局点ID：{{record.siteId}}</a-col>     
				  
				 
        </a-row>
        <a-table
  
          :columns="innerColumns"
          :dataSource="innerData"
          size="middle"
          bordered
          rowKey="1"
          :pagination="false"
          :loading="loading"
          >
        </a-table>
        </div>
        <!--
        <div
        slot="expandedRowRender"
        slot-scope="record"
        style="margin: 0">
	        <a-row
	          :gutter="24"
	          :style="{ marginBottom: '12px' }">
	          <a-col :span="12" v-for="(role, index) in record.permissions" :key="index" :style="{ marginBottom: '12px' }">
	            <a-col :lg="4" :md="24">
	              <span>{{ role.permissionName }}：</span>
	            </a-col>
	            <a-col :lg="20" :md="24" v-if="role.actionEntitySet.length > 0">
	              <a-tag color="cyan" v-for="(action, k) in role.actionEntitySet" :key="k">{{ action.describe }}</a-tag>
	            </a-col>
	            <a-col :span="20" v-else>-</a-col>
	          </a-col>
	        </a-row>
        </div>
        -->
      </a-table>
    	<show-devicesItem ref="ShowDevicesItem"></show-devicesItem>         
    </div>

    <!--<sysCategory-modal ref="modalForm" @ok="modalFormOk"></sysCategory-modal>-->
  </a-card>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import ShowDevicesItem from '@/components/tools/ShowDevicesItem'
  import { getAction,putAction } from '@/api/manage'
  //import SysCategoryModal from './modules/SysCategoryModal'

  
  export default {
    name: "DevicesList",
    mixins:[JeecgListMixin],
    components: {
    	ShowDevicesItem
     // SysCategoryModal
    },
    data () {
      return {
        description: '网络设备管理页面',
        // 表头
        columns: [
          {
            title:'设备ID',
            align:"left",
            dataIndex: 'id'
          },   
          {
            title:'设备名称',
            align:"left",
            dataIndex: 'deviceName'
          },       
          {
            title:'设备类型',
            align:"left",
            dataIndex: 'deviceType'
          },   
          {
            title:'设备描述',
            align:"left",
            dataIndex: 'description'
          },       
           
          {
            title:'设备版本',
            align:"left",
            dataIndex: 'deviceVerison'
          },      
          {
            title:'设备补丁',
            align:"left",
            dataIndex: 'devicePatch'
          },           
          {
            title:'设备IP',
            align:"left",
            dataIndex: 'ipAddress'
          },
          
          {
            title:'最后采集时间',
            align:"left",
            dataIndex: 'collectTime'
          }, 
          {
            title:'状态',
            key: 'tags',
            align:"left",
            dataIndex: 'devStatus',
            scopedSlots: { customRender: 'tags' }
          },
/*          {
            title:'管理',
            key: 'tags1',
            align:"left",
            dataIndex: 'devUpdown',
            scopedSlots: { customRender: 'tags1' }
          
          },    
*/          
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            scopedSlots: { customRender: 'action' },
          }
        ],
        
        // 子表表头
        innerColumns: [
          {
            title: 'license名称',
            dataIndex: 'productName',
            align: "center",
          },
          {
            title: '产品序列号',
            dataIndex: 'productSerialNo',
            align: "center",
          },
          {
            title: '产品版本',
            dataIndex: 'productVersion',
            align: "center",
          },
          {
            title: 'license状态',
            dataIndex: 'licenseState',
            align: "center",
          },
          {
            title: 'license创建',
            align: "center",
            //width: 100,
            dataIndex: 'createTime',
            //key: 'itemText',
          },
          {
            title: 'license到期',
            align: "center",
            dataIndex: 'expiredTime',
          }


        ],
        innerData: [],
    
        
        url: {
          list: "/dev/devices/rootList",
          childList: "/dev/devices/childList",
          delete: "/dev/devices/delete",
          deleteBatch: "/dev/devices/deleteBatch",
          exportXlsUrl: "/dev/devices/exportXls",
          importExcelUrl: "dev/devices/importExcel",
          detailUrl: "dev/devices/importExcel",
          boardInfolist: "/dev/devices/boardInfolist",
          chassisInfolist: "/dev/devices/chassisInfolist",
          suboardInfolist: "/dev/devices/suboardInfolist",
        },
        expandedRowKeys:[],
        hasChildrenField:"hasChild",
        pidField:"id",
        dictOptions:{
        } 
      }
    },
    computed: {
      importExcelUrl(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
      tableProps() {
        let _this = this
        return {
          // 列表项是否可选择
          rowSelection: {
            selectedRowKeys: _this.selectedRowKeys,
            onChange: (selectedRowKeys) => _this.selectedRowKeys = selectedRowKeys
          }
        }
      }
    },
    methods: {
  	
      showDeviceInfo(record){
        //putAction(this.url.detailUrl,{anntId:record.anntId}).then((res)=>{
        //  if(res.success){
            //this.loadData();
         // }
        //});
        this.$refs.ShowDevicesItem.detail(record);
      },	
      loadData(arg){
        if(arg==1){
          this.ipagination.current=1
        }
        this.loading = true
        this.expandedRowKeys = []
        let params = this.getQueryParams()
        return new Promise((resolve) => {
          getAction(this.url.list,params).then(res=>{
            if(res.success){
              let result = res.result
              if(Number(result.total)>0){
                this.ipagination.total = Number(result.total)
                this.dataSource = this.getDataByResult(res.result.records)
                resolve()
              }else{
                this.ipagination.total=0
                this.dataSource=[]
              }
            }else{
              this.$message.warning(res.message)
            }
            this.loading = false
          })
        })
      },
      getDataByResult(result){
        return result.map(item=>{
          //判断是否标记了带有子节点
          if(item[this.hasChildrenField]=='1'){
            let loadChild = { id: item.id+'_loadChild', devName: 'loading...', isLoading: true }
            item.children = [loadChild]
          }
          return item
        })
      },
      handleExpand(expanded, record){
      	//表展开事件
      	
        this.expandedRowKeys=[];
        this.innerData=[];
        if(expanded===true){
        	let params = this.getQueryParams();//查询条件
        	params[this.pidField] = record.id
          this.loading = true;
          //console.log("展开事件"+this.dataSource[1].id);
          this.expandedRowKeys.push(record.id);
          //this.innerData=this.dataSource;
          this.innerData=[
            {
            productName: record.productName,
            productSerialNo: record.productSerialNo,
            productVersion: record.productVersion,
            licenseState: record.licenseState,
            createTime: record.createTime,
            expiredTime: record.expiredTime,            
          	}
          ];
          this.loading = false;
          /*
          getAction(this.url.childList, params).then((res) => {
            if (res.success) {
              this.loading = false;
              this.innerData = res.result;
            }
          });
          */
          
        }     	
        // 判断是否是展开状态
        /*
        if (expanded) {
          this.expandedRowKeys.push(record.id)
          if (record.children.length>0 && record.children[0].isLoading === true) {
            let params = this.getQueryParams();//查询条件
            params[this.pidField] = record.id
            console.log(params);
            getAction(this.url.childList,params).then((res)=>{
              if(res.success){
                if(res.result && res.result.length>0){
                  record.children = this.getDataByResult(res.result)
                  this.dataSource = [...this.dataSource]
                }else{
                  record.children=''
                  record.hasChildrenField='0'
                }
              }else{
                this.$message.warning(res.message)
              }
            })
          }
        }else{
          let keyIndex = this.expandedRowKeys.indexOf(record.id)
          if(keyIndex>=0){
            this.expandedRowKeys.splice(keyIndex, 1);
          }
        }
        */
        
        
      },
      initDictConfig(){
      },
      modalFormOk(formData,arr){
        if(!formData.id){
          this.addOk(formData,arr)
        }else{
          this.editOk(formData,this.dataSource)
          this.dataSource=[...this.dataSource]
        }
      },
      editOk(formData,arr){
        if(arr && arr.length>0){
          for(let i=0;i<arr.length;i++){
            if(arr[i].id==formData.id){
              arr[i]=formData
              break
            }else{
              this.editOk(formData,arr[i].children)
            }
          }
        }
      },
      async addOk(formData,arr){
        if(!formData[this.pidField]){
          this.loadData()
        }else{
          this.expandedRowKeys=[]
          console.log("22222",arr)
          for(let i of arr){
            await this.expandTreeNode(i)
          }
        }
      },
    
      
       
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>